#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include "config.h"
#include "corefuncs.h"

using namespace std;

char hostDir[MAXDIR];
char clientDir[MAXDIR];
char ignoreListFile[MAXDIR];
char relativeMusicDir[MAXDIR];
int ignorePlaylistType;
bool forceConfig;
bool bRemoveUnknown;
bool bVerbose;

void preInitialize()
{
	// The music directory will be refered to as the host
	// If no arguments are provided, it is assumed the current directory is the one being updated
	strcpy( hostDir, "./" );
	forceConfig = false;
	strcpy( relativeMusicDir, "./" );
	ignorePlaylistType = PL_M3U;
	bRemoveUnknown = false;
	bVerbose = true;
}

bool doesHostExist( char *dir ) 
{
	string hostConfig = dir;
	hostConfig += "/.syncSoft/config";
	
	if( fileExists( (char *)hostConfig.c_str() ) )
		return true;
	return false;
}

int createHost( char *dir )
{
	string hostConfigFile = dir;
	hostConfigFile += "/.syncSoft";
	

	mkdir( hostConfigFile.c_str(), 0777 );
	hostConfigFile += "/config";
	
	
	ofstream hostConfig( hostConfigFile.c_str() );
	if( hostConfig.is_open() == true )
	{
		hostConfig << "\"clientDirectory\"\t";
		hostConfig << "\"" << clientDir << "\"" << endl;
		
		hostConfig << "\"ignoreList\"\t";
		switch( ignorePlaylistType )
		{
		case PL_PLS:
			hostConfig << "\"ignore.pls\"" << endl;
		default:
			hostConfig << "\"ignore.m3u\"" << endl;
		}
		hostConfig << "\"clientMusicFolder\"\t" << "\"" << relativeMusicDir << "\"" << endl;
		
	} else {
		cout << "Could Not Create Config File." << endl;
		cout << "Do you have permission to write this directory?" << endl;
		cout << "Try again using sudo." << endl;
		return 0;
	}
	
	hostConfig.close();
	
	return 1;
}

bool fileExists( char *dir )
{
	struct stat sFileInfo;
	
	if( !stat( dir, &sFileInfo ) )
	{	
		return true;
	}
	return false;
	#if 0 
	ifstream checkExist( dir );
	if( checkExist.is_open() == true )
	{
		checkExist.close();
		return true;
	}
	checkExist.close();
	return false;
	#endif
}

ifstream configIn;

void *getNextToken( char *token )
{
	void *ret;
	//char *token = new char[MAXDIR];
	memset( token, 0, sizeof(token) );
	do
	{
		ret = configIn.getline( token, MAXDIR, '\"' );
	//	cout << strlen(token) << endl;
	} while( strlen(token) <= 1 && ret != 0 );
	
//	cout << token << "::" << strlen(token) << " ret: " << ret << endl;

	return ret;
}

int loadConfig( char *dir ) 
{
	string configFile = dir;
	configFile += "/.syncSoft/config";
	//ifstream configIn(configFile.c_str());
	
	configIn.open(configFile.c_str());
	if( configIn.is_open() == false )
		return 0;

	char inToken[MAXDIR] = {0};
//	string inToken;
//	configIn.getline( inToken, MAXDIR, '\"' );
//	while( configIn.getline( inToken, MAXDIR, '\"' ) )
	while( getNextToken( inToken ) )
	{

		//strcpy( inToken, stripQuotes( inToken ) );
		if( !strcmp( inToken, "clientDirectory" ) )
		{
			getNextToken( inToken );
			strcpy( clientDir, inToken );
		}
		
		if( !strcmp( inToken, "ignoreList" ) )
		{
			getNextToken( inToken );
			strcpy( ignoreListFile, inToken );
		}
		if( !strcmp( inToken, "clientMusicFolder" ) )
		{
			getNextToken( inToken );
			strcpy( relativeMusicDir, inToken );
		}
		
	}
	
	configIn.close();
	return 1;
}

char *stripQuotes( const char *str )
{
	char *noQuotes = new char[strlen(str)];
	memset( noQuotes, 0, sizeof( noQuotes ) );
	int j = 0;
	for( int i = 1; i < strlen( str ) - 1; i++ )
	{
		noQuotes[j] = str[i];
		j++;
	}
	return noQuotes;
}

